package com.qf.service.impl;

import com.qf.constans.ResultCode;
import com.qf.dto.R;
import com.qf.entity.CheckType;
import com.qf.entity.TNotify;
import com.qf.events.UpdateNotifyEvent;
import com.qf.exceptions.AddDataException;
import com.qf.mapper.NotifyMapper;
import com.qf.service.NotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
杨威
2021/10/30
9:22
*/
@Service
@Transactional
public class NotifyServiceImpl implements NotifyService {
    private NotifyMapper notifyMapper;
    private ApplicationContext context;//增删改要调事件 更新缓存
    @Autowired
    public void setNotifyMapper(NotifyMapper notifyMapper) {
        this.notifyMapper = notifyMapper;
    }
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public void updateNotify(TNotify tNotify) {
        notifyMapper.updateNotify(tNotify);
        context.publishEvent(new UpdateNotifyEvent());
    }

    @Override
    public void addNotify(TNotify tNotify) {
        if(tNotify.isNull(CheckType.ADD)){
            throw new AddDataException("数据不能为空",ResultCode.DATA_ISNULL);
        }
        notifyMapper.addNotify(tNotify);
        context.publishEvent(new UpdateNotifyEvent());
    }

    @Override
    public void deleteNotify(Long id) {
        notifyMapper.deleteNotify(id);
        context.publishEvent(new UpdateNotifyEvent());
    }
}
